package cn.yotta.user.core.server.iface.login.impl;

import cn.yotta.user.api.params.login.BaseLoginRequest;
import cn.yotta.user.api.params.login.LoginByUsernamePasswordRequest;
import cn.yotta.user.core.server.dto.user.UserDto;
import cn.yotta.user.core.server.exception.user.UserNotFindException;
import cn.yotta.user.core.server.iface.login.PasswordEncoder;
import cn.yotta.user.core.server.manager.captcha.CaptchaManager;
import cn.yotta.user.core.server.manager.user.UserManager;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wyaoyao
 * @date 2022/11/9 8:33
 */
@Slf4j
public class UsernamePasswordLoginProvider extends AbstractPasswordLoginProvider<LoginByUsernamePasswordRequest> {

    private final UserManager userManager;

    public UsernamePasswordLoginProvider(UserManager userManager, CaptchaManager captchaManager, PasswordEncoder passwordEncoder, Boolean skipCheck) {
        super(passwordEncoder, captchaManager, skipCheck);
        this.userManager = userManager;
    }


    @Override
    protected UserDto loadUser(String principal) throws UserNotFindException {
        return userManager.findByUsername(null, principal);
    }


    @Override
    public Boolean support(BaseLoginRequest request) {
        return request.getClass().equals(LoginByUsernamePasswordRequest.class);
    }
}
